package com.map;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.micromata.opengis.kml.v_2_2_0.Kml;

public class PointOperatorServlet extends HttpServlet { 
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		if(method.equalsIgnoreCase("insert")){
			String featureJson = request.getParameter("feature");
			featureJson = URLDecoder.decode(featureJson,"UTF-8");
			PointOperatorFileImp pointOperatorFileImp = new PointOperatorFileImp(getServletContext().getRealPath("/")+"aaa.kmz");
			PointOperator pointOperator = pointOperatorFileImp;
			response.getWriter().print(pointOperator.insert(featureJson));
		}
		if(method.equalsIgnoreCase("query")){
			response.setContentType("text/xml;charset=UTF-8");
			String queryInfo = request.getParameter("queryInfo");
			queryInfo = URLDecoder.decode(queryInfo,"UTF-8");
			PointOperatorFileImp pointOperatorFileImp = new PointOperatorFileImp(getServletContext().getRealPath("/")+"aaa.kmz");
			PointOperator pointOperator = pointOperatorFileImp;
			Kml kml = pointOperator.query(queryInfo);
			kml.marshal(response.getWriter());
		}
		if(method.equalsIgnoreCase("test")){
			PointOperatorFileImp pointOperatorFileImp = new PointOperatorFileImp(getServletContext().getRealPath("/")+"aaa.kmz");
			pointOperatorFileImp.getKml().marshal(response.getWriter());
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
}
